$(document).ready(function(){
    $('.delete').click(function(){
        $.deleteRow($(this));
    });
    
    $('.search').click(function(){
        $.search($(this));
    });

    $('#call').click(function(){
        $.call();
    });

    $('.statData').click(function(){
        $.statData($(this).attr('id'));
    });
    $.statData('day');
}); 

$.extend({

    /**
     * 获取统计数据
     */
    statData: function (type) {
        var obj = $('#'+type);
        var objs = obj.parent().find('a');
        objs.removeClass('btn-info').addClass('btn-default');
        obj.removeClass('btn-default').addClass('btn-info');
        var url = 'http://local.backend.queue.com/queues/stat-data';
        $.post(url, {type: type}, function(t){
            switch(type){
                case 'day': $txt = '日'; break;
                case 'week': $txt = '周'; break;
                case 'month': $txt = '月'; break;
                case 'year': $txt = '年'; break;
            }
            config.options.title.text = '排队走势表('+$txt+')';
            $.t = t;
            setTimeout($.delData(), 30);
            setTimeout($.addData(), 30);
        }, 'json');
    },

    addData: function (){
        dataLabels.push($.t.labels.pop());
        dataQueued.push($.t.dataQueued.pop());
        dataIn.push($.t.dataIn.pop());
        dataPass.push($.t.dataPass.pop());
        inRates.push($.t.inRates.pop());
        passRates.push($.t.passRates.pop());
        chart.update();
        if($.t.labels.length > 0){
            setTimeout($.addData(), 30);
        }
    },

    delData: function(){
        dataLabels.shift();
        dataQueued.shift();
        dataIn.shift();
        dataPass.shift();
        inRates.shift();
        passRates.shift();
        chart.update();
        if(dataLabels.length > 0){
            setTimeout($.delData(), 30);
        }
    },

    /**
     * 叫号
     */
    call: function () {
        var total = $('#callTotal').val();
        var api = 'http://local.backend.queue.com/queues/call';
        $.post(api, {total: total}, function(t){
            var cls = t.code == 0 ? 'text-danger box-danger' : 'text-success box-success';
            $('#currentQueueNumber').html(t.result.data.currentQueueNumber)
            $('#todayQueueNumber').html(t.result.data.todayQueueNumber)
            $('#todayCallNumber').html(t.result.data.todayCallNumber)
            $('#todayPassNumber').html(t.result.data.todayPassNumber)
            $('#todayInNumber').html(t.result.data.todayInNumber)
            $('#showMsg').attr('class', cls).html(t.msg);
        }, 'json');
    },

    /**
     * 删除行
     * @param {type} obj
     * @returns {undefined}
     */
    deleteRow: function (obj) {
        if(confirm('您确认要这些条目吗？')){
            var selectIds = this._getSelectIds();
            var data = {ids: selectIds};
            var url = obj.attr('url');
            $.post(url, data, function(t){
                for(var i in selectIds){
                    var o =  $('tr[data-key="'+selectIds[i]+'"]');
                    o.fadeOut(500, function(){o.remove();});
                }
            }, 'html');
        }
    },
    
    /**
     * 搜索
     * @returns {Array}
     */
    search: function (obj) {
        var o = obj.parent().prev();
        var data = 'keyword=' + o.val();
        var url = obj.attr('url') + '?' + data;
        window.location.href = url;
    },
    
    /**
     * 取得选中
     * @returns {Array}
     */
    _getSelectIds: function () {
        var objs = $('input[name="selectIds[]"]');
        var ids = [];
        var n = 0;
        for(var i = 0; i < objs.length; i++){
            if($(objs[i]).prop('checked')){
                ids[n] = $(objs[i]).val();
                n++;
            }
        }
        return ids;
    }
});

